define(['jquery', 'bootstrap', 'backend', 'table', 'form','qrcode'], function ($, undefined, Backend, Table, Form,Qrcode) {

    //中文转码
    function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for(i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
            }
        }
        return out;
    }
    $('.qrcod_data').each(function(index,value){
        var qrcode_txt =  $('.qrcode_txt').eq(index).val();
        $('.qrcode').eq(index).qrcode({render:'canvas',width: 130,height: 130,text:utf16to8(qrcode_txt)});
    })
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'information/warehouse/index' + location.search,
                    add_url: 'information/warehouse/add',
                    edit_url: 'information/warehouse/edit',
                    del_url: 'information/warehouse/del',
                    multi_url: 'information/warehouse/multi',
                    import_url: 'information/warehouse/import',
                    table: 'warehouse',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'stocker_id', title: __('Stockerid'), operate: false},
                      //  {field: 'switch', title: __('Switch'), searchList: {"0":__('Switch 0'),"1":__('Switch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'switch', title: __('Switch'), searchList: {"0":__('Switch 0'),"1":__('Switch 1')}, table: table, formatter: Table.api.formatter.normal},
                        //{field: 'source', title: __('Source'), searchList: {"1":__('Source 1')," 2":__('Source  2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('#print-btn').click(function(){
                let allids =  Controller.api.selectedids(table);
                if(allids.length==0) return Toastr.error("请先选择仓库");   
                Fast.api.open('information/warehouse/print?warehouse_id='+allids,'打印仓库信息');
            })

            $('#print-btn-good').click(function(){
                let allids =  Controller.api.selectedids(table);
                if(allids.length==0) return Toastr.error("请先选择仓库");   
                Fast.api.open('information/inventory/print?inventory_id='+allids+'&type=1','打印仓库信息');
            })

            $('.btn-danger').on('click',function(){
            $.post('information/warehouse/getData',function(e){
                   if(e.code==1){
                     Toastr.success(e.msg);
                     table.bootstrapTable('refresh');
                   }else{
                     Toastr.error(e.msg);
                   }
                })
            })
            
            $(table).data("operate-del", null);    
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'information/warehouse/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'information/warehouse/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'information/warehouse/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        choose:function(){
            $('#choose_id').click(function(){
                let id =  Controller.api.selectedids(chooseTable);
                Fast.api.close(id);
            })
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var chooseTable = $('#chooseTable')

            chooseTable.bootstrapTable({
                url: "information/warehouse/choose" + location.search,
                pk: 'id',
                search:false,
                singleSelect:true,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                      //  {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                      //  {field: 'switch', title: __('Switch'), searchList: {"0":__('Switch 0'),"1":__('Switch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'source', title: __('Source'), searchList: {"1":__('Source 1')," 2":__('Source  2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(chooseTable);
            
        },
        api: {
             //获取选中的行
             selectedids: function (table, current) {
                var options = table.bootstrapTable('getOptions');
                //如果有设置翻页记忆模式
                if (!current && options.maintainSelected) {
                    return options.selectedIds;
                }
                return $.map(table.bootstrapTable('getSelections'), function (row) {
                    return row[options.pk];
                });
            },

            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
